#!/usr/bin/env bash

#声名应用变量部分

export APP_DOMAIN=runhua.com
export APP_USER=zhgk
export APP_PACKAGE=apprepo
export APP_NAME=appRepo
# app类型 API、WEB
export APP_TYPE=WEB


export DB_TYPE=MYSQL
export DB_HOST=127.0.0.1
export DB_PORT=3306
export DB_NAME=db
export DB_USER=root


# 声明GOPATH
export GOPATH=$HOME/golib/
# 安装Web框架
go get -u -v github.com/gin-gonic/gin
# 安装数据库框架
go get -u -v github.com/jinzhu/gorm
# 安装redis框架
go get -u -v github.com/garyburd/redigo/redis
# 安装mongodb框架
go get -u -v gopkg.in/mgo.v2
# 安装mysql框架
go get -u -v github.com/go-sql-driver/mysql


# 编译逻辑

# 创建初始化文件夹
if [ ! -d "$PWD/src" ]; then
    mkdir $PWD/src
fi

if [ ! -d "$PWD/src/$APP_DOMAIN" ]; then
    mkdir $PWD/src/$APP_DOMAIN
fi

if [ ! -d "$PWD/src/$APP_DOMAIN/$APP_USER" ]; then
    mkdir $PWD/src/$APP_DOMAIN/$APP_USER
fi

if [ ! -d "$PWD/src/$APP_DOMAIN/$APP_USER/$APP_PACKAGE" ]; then
    mkdir $PWD/src/$APP_DOMAIN/$APP_USER/$APP_PACKAGE
fi

# 生成主程序代码

if [ ! -f "$PWD/src/$APP_DOMAIN/$APP_USER/$APP_PACKAGE/main.go" ]; then
    touch $PWD/src/$APP_DOMAIN/$APP_USER/$APP_PACKAGE/main.go
    startFile=$PWD/src/$APP_DOMAIN/$APP_USER/$APP_PACKAGE/main.go
    echo "package main" > $startFile
    echo "" >> $startFile
    echo "" >> $startFile
    echo "// 启动函数" >> $startFile
    echo "func main () {" >> $startFile
    echo "  println(\"初始化应用成功\") " >> $startFile
    echo "" >> $startFile
    echo " }" >> $startFile

fi



if [ ! -d "$PWD/release" ]; then
    mkdir $PWD/release
fi
# web文件如果存在则打包， 否则忽略
if [ $APP_TYPE = "WEB" ] ; then
    if [ ! -d "$PWD/web" ]; then
        mkdir $PWD/web
        mkdir $PWD/web/static
        mkdir $PWD/web/view
    fi
fi

# 生成配置文件

if [ ! -f "$PWD/config.yaml" ]; then
    touch $PWD/config.yaml
    echo "APP_NAME: "$APP_NAME  > $PWD/config.yaml
    echo "APP_NAME: "$APP_TYPE >> $PWD/config.yaml
    echo "APP_DB: " >> $PWD/config.yaml
    echo "  TYPE: " $DB_TYPE >> $PWD/config.yaml
    echo "  HOST: " $DB_HOST  >> $PWD/config.yaml
    echo "  PORT: " $DB_PORT  >> $PWD/config.yaml
    echo "  USER: " $DB_USER >> $PWD/config.yaml
    echo "  PASS: " $DB_PASS  >> $PWD/config.yaml
    echo "  SCHEMA: " $DB_NAME  >> $PWD/config.yaml
    echo "  MAX_CON: 10 "   >> $PWD/config.yaml
    echo "  MAX_IDLE: 5 "   >> $PWD/config.yaml
    echo "  URL: $DB_USER:$DB_PASS@tcp($DB_HOST:$DB_PORT)/$DB_NAME?charset=utf8&parseTime=True&loc=Local" >> $PWD/config.yaml
fi

